package com.jt.www.biz.order_pc.service.impl;

import com.jt.www.biz.order_pc.service.SdCheckService;
import com.jt.www.dao.mapper.sdrisk.SdBankCardMapper;
import com.jt.www.exception.BizException;
import com.jt.www.model.reps.GenericListResponse;
import com.jt.www.model.reps.GenericResponse;
import com.jt.www.model.sdCheck.CheckInsureDTO;
import com.jt.www.model.sdcard.SdBankCardEntity;
import com.jt.www.model.vo.sdCheck.EndorsementsInfo;
import com.jt.www.remote.PolClient;
import com.jt.www.remote.ProductClient;
import com.jt.www.remote.ins.InsClient;
import com.jt.www.util.JsonUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Description: 校验山东重复投保
 * @author: xuxinku
 * @Date: 2019/6/14 16:21
 * @ModifiedDate：
 * @Copyright:江泰保险股份有限公司
 */
@Service
public class SdCheckServiceImpl implements SdCheckService {

    public static final Logger log = LoggerFactory.getLogger(SdCheckServiceImpl.class);

    @Autowired
    private InsClient insClient;

    @Autowired
    private PolClient polClient;

    @Autowired
    private ProductClient productClient;

    @Autowired
    private SdBankCardMapper sdBankCardMapper;

    @Override
    public ResponseEntity checkInsure(String holderName, String insuredName, Date startDate) {
        CheckInsureDTO checkinsureDTO = new CheckInsureDTO();
        checkinsureDTO.setUserCode(holderName);
        ResponseEntity<GenericListResponse<Map<String, Object>>> genericListResponseResponseEntity = null;
        try {
            log.debug("调用投保中心;查询保单=======run===》");
            String s = JsonUtils.toJson(checkinsureDTO);
            genericListResponseResponseEntity = insClient.checkInsure(s);
            log.debug("调用投保中心:查询保单=======end===》");
        } catch (Exception e) {

            throw new BizException("调用投保中心:查询保单失败");
        }

        GenericListResponse<Map<String, Object>> body = genericListResponseResponseEntity.getBody();
        List<Map<String, Object>> list = body.getList();
        if (null != list) {
            for (Map<String, Object> pol : list) {
                String polStatus = "";
                String polEndDate = null;
                String policyNo = "";
                String insuredName1 = "";
                for (String key : pol.keySet()) {
                    //校验传来的key是否存在map中
                    polStatus = pol.get("polStatus") + "";
                    polEndDate = pol.get("polEndDate") + "";
                    policyNo = pol.get("policyNo") + "";
                    insuredName1 = pol.get("insuredName") + "";
                }

                log.debug("保单状态==========》" + polStatus);
                log.debug("保单止期==========》" + polEndDate);
                log.debug("保单号==========》" + policyNo);
                log.debug("被保险人名称==========》" + insuredName1);
                SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
                Date endDates = null;
                try {
                    endDates = dateFormat.parse(polEndDate);
                } catch (ParseException e) {
                    e.printStackTrace();
                    throw new BizException("日期转换失败");
                }
                ResponseEntity<GenericListResponse<EndorsementsInfo>> genericListResponseResponseEntity1 = null;
                try {
                    log.debug("调用批单中心;查询批单=======run===》");
                    genericListResponseResponseEntity1 = polClient.validateSubmitEndorsement(policyNo, "edu");
                    log.debug("调用批单中心;查询批单=======end===》");
                } catch (Exception e) {
                    e.printStackTrace();
                    throw new BizException("调用批单中心:查询批单状态失败");
                }

                boolean flag1 = false;
                if (insuredName1.equals(insuredName)) {
                    flag1 = true;
                }

                boolean flag2 = true;
                //log.debug("校验批单返回数据"+genericListResponseResponseEntity1.getBody().getList());
                //if(null!=genericListResponseResponseEntity1.getBody()){
                List<EndorsementsInfo> PDlist = genericListResponseResponseEntity1.getBody().getList();
                for (EndorsementsInfo pd : PDlist) {
                    String endorsementsType = pd.getEndorsementsType();
                    String status = pd.getStatus();
                    log.debug("批单状态=====》" + endorsementsType);
                    log.debug("批单是否生效=====》" + status);

                    if ("04".equals(endorsementsType) && "03".equals(status)) {
                        flag2 = false;
                        break;
                    }
                }
                //}
                //校验规则   [保单状态 00 起期 止期不能重叠 被保险人不能相同 批单状态不能为 04 ]
                int i = endDates.compareTo(startDate);
                if (!StringUtils.isEmpty(polStatus)) {
                    if (polStatus.equals("00") && i >= 0 && flag1 && flag2) {

                        throw new BizException("提示语：查询到已完成投保的保单信息，请勿重复投保！");

                    }
                }


            }

        }

        return GenericResponse.ok();
    }

    @Override
    public List<SdBankCardEntity> queryBankCard(String appCode) {
        SdBankCardEntity sdBankCardEntity = new SdBankCardEntity();
        sdBankCardEntity.setAppCode(appCode);
        return sdBankCardMapper.select(sdBankCardEntity);
    }
}
